/*
 * Copyright (C), 2002-2016, 上海亦寻信息技术有限公司
 * FileName: RetainServiceImp.java
 * Author:   linyao
 * Date:     2016-7-4 下午2:46:36
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.website.service.marketing;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.model.PriceSchemeDetail;
import com.emm.yixun.common.model.dto.RetainDto;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.marketing.service.PriceSchemeDetailServiceFacade;
import com.emm.yixun.marketing.service.RetainServiceFacade;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author linyao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class RetainServiceImp implements RetainService {
	@Autowired
	private RetainServiceFacade retainServiceFacade;
	@Autowired
	private PriceSchemeDetailServiceFacade schemeDetailServiceFacade;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.emm.yixun.website.service.marketing.RetainService#getRetainDtoList(com.emm.yixun.common.model.dto.RetainDto,
	 * java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public Response<Page<RetainDto>> getRetainDtoList(RetainDto reqDto, Integer pageSize, Integer pageNum) {
		if (StringUtils.isNotBlank(reqDto.getM_businessStatus())) {
			String sql = new String(" and t.retain_type in (");
			String[] status = reqDto.getM_businessStatus().split(",");
			String sbsql = "";
			for (int i = 0; i < status.length; i++) {
				sbsql += status[i];
				if (i != status.length - 1) {
					sbsql += ",";
				}
			}
			sql += sbsql + ")";
			reqDto.setM_businessStatus(sql);
		}
		Response<Page<RetainDto>> response = retainServiceFacade.getRetainDtoList(reqDto, pageSize, pageNum);
		if (response.isSuccess() && response.getEntity() != null && response.getEntity().getRows() != null
				&& response.getEntity().getRows().size() > 0) {
			List<RetainDto> retainDtos = response.getEntity().getRows();
			for (RetainDto retainDto : retainDtos) {
				Response<PriceSchemeDetail> response2 = schemeDetailServiceFacade.selectEntityByRoomId(retainDto
						.getRoomId());
				if (response2.isSuccess() && response2.getEntity() != null) {
					retainDto.setTotalPrice(response2.getEntity().getTotalPrice());
				}
			}

		}
		return response;
	}
	
	@Override
	public List<RetainDto> queryRetainListExport(RetainDto reqDto) {
		reqDto.setIsExport("1");
		Response<Page<RetainDto>> response = getRetainDtoList(reqDto, 10, 1);
		return response.getEntity().getRows();
	}
}
